/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.model;

import com.fandi.dao.TransactionOutDao;
import com.fandi.dao.impl.TransactionOutDaoImpl;
import com.fandi.entity.Item;
import com.fandi.entity.TransactionOut;
import com.fandi.listener.TransactionOutListener;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fandiak
 */
public class TransactionOutModel extends TransactionOut {
    
    private TransactionOutListener listener;

    public TransactionOutListener getListener() {
        return listener;
    }

    public void setListener(TransactionOutListener listener) {
        this.listener = listener;
    }
    
    @Override
    public void setId(int id) {
        super.setId(id); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setTransCode(String transCode) {
        super.setTransCode(transCode); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setItems(List<Item> items) {
        super.setItems(items); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setDescription(String description) {
        super.setDescription(description); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setTransDate(Date transDate) {
        super.setTransDate(transDate); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setLastUpd(Date lastUpd) {
        super.setLastUpd(lastUpd); //To change body of generated methods, choose Tools | Templates.
        fireOnchange();
    }

    @Override
    public void setDestination(String destination) {
        super.setDestination(destination); //To change body of generated methods, choose Tools | Templates.
    }
    
    public String getLastTransCode() {
        try {
            TransactionOutDao dao = new TransactionOutDaoImpl();
            return dao.lastTransCode();
        } catch (SQLException ex) {
            Logger.getLogger(TransactionOutModel.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public List<TransactionOutModel> selectAll() {
        try {
            TransactionOutDao dao = new TransactionOutDaoImpl();
            List<TransactionOutModel> list = dao.selectAll();
            
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(TransactionOutModel.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public int count() {
        try {
            TransactionOutDao dao = new TransactionOutDaoImpl();
            return dao.count();
        } catch (SQLException ex) {
            Logger.getLogger(TransactionOutModel.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }
    
    public int insert() {
        try {
            TransactionOutDao dao = new TransactionOutDaoImpl();
            TransactionOutModel model = new TransactionOutModel();
            
            model.setTransCode(getTransCode());
            model.setDestination(getDestination());
            model.setDescription(getDescription());
            model.setTransDate(getTransDate());
            
            return dao.insert(model);
        } catch (SQLException ex) {
            Logger.getLogger(TransactionOutModel.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }
    
    public void reset() {
        setDestination("");
        setDescription("");
    }
    
    private void fireOnchange() {
        if (listener != null) {
            listener.onChange(this);
        }
    }
}
